/**
 * 自动创建的对应js目录
 * 创建时间：2018-05-18 17:33:14
 * 技术支持：曹传喜
 * 邮箱：15075107120@139.com
 */
$(function () {
    initDatagrid();
});

//初始化表格
function initDatagrid() {
    var url = $.util.getRequestJson('/json/rulescfg.typeList.json');
    var listTable = $('#listTable').datagrid({
        url: url,
        idField: 'id',
        fit: true,
        pagination: true,
        pageSize: 20,
        checkOnSelect: false,
        selectOnCheck: false,
        singleSelect: true,
        fitColumns: true,
        toolbar: '#toolbarButton',
        columns: [[
            {
                field: 'sort',
                align: 'center',
                title: '优先级'
            },
            {
                field: 'name',
                title: '分类名称'
            },
            {
                field: 'sql',
                width: 1,
                title: '分类sql'
            },
            {
                field: 'remark',
                title: '备注信息'
            }, {
                field: 'id',
                title: '操作',
                align: 'center',
                formatter: function (value, row) {
                    var aEl = '<a href=javascript:edit("{0}"); class="easyui-tooltip iconImg icon-edit" title="编辑"></a>';
                    aEl += '&nbsp;&nbsp;<a href=javascript:updateStatus("{0}",-1); class="easyui-tooltip iconImg icon-remove" title="删除"></a>';
                    if (row.status == 1) {
                        aEl += '&nbsp;&nbsp;<a href=javascript:updateStatus("{0}",0); class="easyui-tooltip iconImg icon-disable" title="禁用"></a>';
                    } else {
                        aEl += '&nbsp;&nbsp;<a href=javascript:updateStatus("{0}",1); class="easyui-tooltip iconImg icon-ok" title="启用"></a>';
                    }
                    //格式化操作按钮
                    return $.string.format(aEl, value);
                }
            }
        ]]
    });
}

function edit(id) {
    var title = id ? '编辑客户信息类型' : '添加客户信息类型';
    var url = $.util.getRootPath() + '/page/rulescfg/showcfg/editType.html';
    url = id ? url + '?id=' + id : url;
    var dialog = $.easyui.showDialog({
        title: title,
        width: 600,
        height: 400,
        maximizable: true,
        href: url,
        topMost: true,
        saveButtonText: '保存',
        onSave: function () {
            subForm(dialog);
        },
        buttons: [{
            text: '测试sql',
            handler: function () {
                validate();
            }
        }],
        onLoad: function () {
            if (id) {
                var row = $('#listTable').datagrid('findRow', id);
                parent.$('#showCfgTypeForm').form('load', row);
            }
        }
    });
}

function subForm(dialog) {
    var url = $.util.getRootPath() + '/rulescfg/saveType';
    var form = parent.$('#showCfgTypeForm').form('submit', {
        url: url,
        onSubmit: function () {
            return parent.$(this).form('validate');
        },
        success: function (data) {
            if (data.success) {
                $('#listTable').datagrid('reload');
                dialog.dialog('close');
            }
            $.easyui.messager.show(data.message);
        }
    });
}

function validate() {
    var url = $.util.getRequestJson('/json/rulescfg.validateShow.json');
    var param = {sql: parent.$('#sql').val()};
    $.util.post(url, param, function (json) {
        if (json.success) {
            $.easyui.messager.show("测试通过！");
        } else {
            $.easyui.messager.show(json.message);
        }
    });
}

function updateStatus(id, status) {
    var messsageStr = '删除成功';
    var msg = '确定要删除吗？';
    if (status == 1) {
        msg = '确定要启用吗？';
        messsageStr = '启用成功';
    } else if (status == 0) {
        msg = '确定要禁用吗？';
        messsageStr = '禁用成功';
    }
    $.easyui.messager.confirm(msg, function (r) {
        if (r) {
            var url = $.util.getRequestJson('/json/rulescfg.updateStatus.json');
            var param = {id: id, status: status};
            $.util.post(url, param, function (json) {
                if (json.success) {
                    $('#listTable').datagrid('reload');
                    $.easyui.messager.show(messsageStr);
                    return;
                }
                $.easyui.messager.show(json.message);
            });
        }
    });
}

